<?php

namespace Modules\Activity;

use Modules\Activity\Entities\Lottery\Activity as LotteryActivity;

use Modules\Activity\Entities\ActivityOrder;

// use \Modules\Activity\Entities\YiFanShang\Activity as YiFanShangActivity;

class Cron
{
	public function handle($schedule)
	{
		$that = $this;
		$schedule->call(function() use ($that) {
			try {
				$that->everyMinute();
			}
			catch (\Throwable $e) {
                \Log::error('activity模块 cron任务异常');
                \Log::error($e->getMessage());
            }
		})->everyMinute();
	}

	public function everyMinute()
	{
		// \Log::error('活动结束清算');

		// 抽奖活动清算
		$list = LotteryActivity::status('working')->whereNull('end_at')->get();
		foreach ($list as $item) {
		    // 当日流水满*自动报名
			$item->waterFullAuto();
			$item->checkStatus();
		}

		// 待发货的订单再执行一次创建package
		$list = ActivityOrder::status('deliver_pending')->whereNotNull('node_type')->get();
		foreach ($list as $order) {
			$order->paidListenerSync();
		}

		// 更新有库存的一番赏房间
		// $list = YiFanShangActivity::where('stock', '>', 0)->get();
		// foreach ($list as $item) {
		// 	$item->update([
	        //  'stock' => $item->rooms()->sum('stock')
	        // ]);
		// }

	}
}
